class Department < ActiveRecord::Base

  hobo_model
  
  acts_as_ferret :fields => ["name", "description"]
  
  acts_as_reportable :only => ['name']

  has_many  :documents

  def self.most_recent
    find(:all, :limit => 5, :order => 'updated_at DESC')
  end
  
  def self.find_sorted_by_documents
    find(:all, :include => 'documents', :order => 'COUNT(documents.*)' )
  end
  
   def self.full_text_search(q, options = {})
     return nil if q.nil? or q==""
     default_options = {:limit => 10, :page => 1}
     options = default_options.merge options
     
     # get the offset based on what page we're on
     options[:offset] = options[:limit] * (options.delete(:page).to_i-1)  
     
     # now do the query with our options
     results = self.find_by_contents(q, options)
     return [results.total_hits, results]
   end
  

  # --- Hobo Permissions --- #

  def creatable_by?(creator)
    not creator.guest?
  end

  def updatable_by?(updater, new)
    not updater.guest?
  end

  def deletable_by?(deleter)
    false
  end

  def viewable_by?(viewer, field)
    true
  end

end
